<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\CreateLogisticsTemplate;
use App\Admin\Repositories\Logistics;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Layout\Content;

class LogisticsController extends AdminController
{
    protected function grid()
    {
        return Grid::make(new Logistics(), function (Grid $grid) {
            $grid->id->sortable();
            $grid->name;

            $grid->logo->image('', '50');
            $grid->number;
            $grid->order->sortable();
            $grid->contact_phone;
            $grid->status->display(function ($value) {
                return $value ? '是' : '否';
            });
            $grid->is_default->display(function ($value) {
                return $value ? '是' : '否';
            });
            $grid->created_at;
            $grid->updated_at->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->actions(function ($actions) {
                $actions->append(new CreateLogisticsTemplate());
            });
        });
    }

    protected function form()
    {
        return Form::make(new Logistics(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('number');
            $form->image('logo');
            $form->mobile('contact_phone');
            $form->number('order')->default(100);
            $form->radio('status')->options([0 => '否', 1 => '是'])->default(1);
            $form->radio('is_default')->options([0 => '否', 1 => '是'])->default(0);
        });
    }

    public function show($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description()['show'] ?? trans('admin.show'))
            ->body(view('admin.logistics.show', [
                'logistics' => \App\Models\Logistics::query()->with('templates')->find($id)
            ]));
    }
}
